package com.efast.dmom.mgmt.controller.les.pda;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.entity.les.TwDocOrderDetails;
import com.efast.dmom.mgmt.entity.les.TwDocOrderHeader;
import com.efast.dmom.mgmt.service.les.TwDocOrderDetailsService;
import com.efast.dmom.mgmt.service.les.TwDocOrderHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;

/**
 * 出库订单
 * 
 * 设计到的表： a) 出库订单表TW_DOC_ORDER_HEADER、TW_DOC_ORDER_DETAILS b) 分配表 TW_ACT_ALLOC_LOT_D c) 拣货任务表
 * TW_OUTB_TASK_HEADER、TW_OUTB_TASK_DETAIL d) 库存表TW_INV_SKU_LOC_T_L
 * 
 * @author chlp
 */
@Controller
@RequestMapping(value = "/pdaTwDocOrderHeader")
@Api(description = "pda-->出库-->整单发运")
public class PdaTwDocOrderHeaderController {

	private final TwDocOrderHeaderService twDocOrderHeaderService;
	private final TwDocOrderDetailsService twDocOrderDetailsService;

	@Autowired
	public PdaTwDocOrderHeaderController(TwDocOrderHeaderService twDocOrderHeaderService,
			TwDocOrderDetailsService twDocOrderDetailsService) {
		this.twDocOrderHeaderService = twDocOrderHeaderService;
		this.twDocOrderDetailsService = twDocOrderDetailsService;
	}

	/**
	 * 【保存】 出库订单header的新增 后台需要生成：SO编号/SO状态/创建人/创建人类型/创建时间
	 * 
	 * @param twDocOrderHeader
	 *            twDocOrderHeader
	 */
	@ApiOperation(value = "/查询数据")
	@RequestMapping(value = "/queryData", method = RequestMethod.POST)
	public @ResponseBody ResultBean update(@RequestBody TwDocOrderHeader twDocOrderHeader) {
		ResultBean result = new ResultBean(true, "'");
		TwDocOrderHeader twDocOrderHeader1 = twDocOrderHeaderService.selectTwDocOrderHeaderByObj(twDocOrderHeader);
		List<TwDocOrderDetails> twDocOrderDetails = twDocOrderDetailsService.selectTwDocOrderDetails(
				new Assist(Assist.andEq("tw_doc_order_header_id", twDocOrderHeader1.getTwDocOrderHeaderId())));
		if (twDocOrderDetails.size() == 0) {
			throw new ServiceException("没有查询到数据");
		}
		result.setObject(twDocOrderDetails);
		return result;
	}

	/**
	 * 【确认发运】--Shipment
	 * 
	 * @param twDocOrderHeader
	 *            twDocOrderHeader
	 */
	@RequestMapping(value = "/shipment", method = RequestMethod.POST)
	public @ResponseBody ResultBean shipment(@RequestBody TwDocOrderHeader twDocOrderHeader) {
		ResultBean result = new ResultBean(true, "'");
		List<TwDocOrderDetails> list = new ArrayList<>();
		System.out.println();
		twDocOrderHeaderService.shipment(twDocOrderHeader,list);
		result.setMessage("发运成功");
		return result;
	}

}
